﻿using Acme.BookStore.Goods;
using Acme.BookStore.Order;
using Acme.BookStore.RBAC;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.HousingPriceplan
{
    /// <summary>
    /// This is the HousingShow class.
    /// </summary>
    /// 
    [ApiExplorerSettings(GroupName = "v2")]
    public class HousingShow : ApplicationService, IHousingShow
    {

        IRepository<RatePlans> _RatePlansReservationsrepository;
        IRepository<RoomType> _RoomTypeTypesrepository;

        public HousingShow(IRepository<RatePlans> ratePlansReservationsrepository, IRepository<RoomType> roomTypeTypesrepository)
        {
            _RatePlansReservationsrepository = ratePlansReservationsrepository;
            _RoomTypeTypesrepository = roomTypeTypesrepository;
        }
        /// <summary>
        /// 房价显示
        /// </summary>
        /// <param name="RoomTypeId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> HousinShow(string? RoomName)
        {
            var RatePlanss = await _RatePlansReservationsrepository.GetQueryableAsync();
            var RoomTypes = await _RoomTypeTypesrepository.GetQueryableAsync();
            var list = from a in RatePlanss
                       join b in RoomTypes on a.RoomTypeId equals b.Id
                       where a.IsDeleted == false
                       select new IHousingShowDTO()
                       {

                           ProductName  = a.ProductName,
                           RoomName = b.RoomName,
                           BreakfastNumber = a.BreakfastNumber,
                           SalesStrategy =a.SalesStrategy,
                           PayoutMethods =a.PayoutMethods,
                           RoomPrice = b.RoomPrice,
                           RoomTypeId = b.Id,
                           StandardPrice  = a.StandardPrice,
                           AccommodationOffers = a.AccommodationOffers,
                           MembershipSpread = a.MembershipSpread,
                           NumberRooms = a.NumberRooms,
                           Id = a.Id,

                       };

            if (!string.IsNullOrEmpty(RoomName))
            {
                list = list.Where(x => x.RoomName ==RoomName);
            }
            var resultList =  list.ToList();
            return ApiResult.Ok(resultList);

        }

        /// <summary>
        /// 房价删除
        /// </summary>
        /// <param name="RoomTypeId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> HousinDelect(Guid Id)
        {
            await _RatePlansReservationsrepository.DeleteAsync(x => x.Id == Id);
            return ApiResult.Ok();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("HousinUpdate")]
        public async Task<ApiResult> HousinUpdate(IHousingUpdateDTO dto)
        {
            var entity = (await _RatePlansReservationsrepository.GetQueryableAsync()).Where(x => x.Id == dto.Id);
            if (entity.Count() == 0)
            {
                return ApiResult.Fail("没有找到");
            }
            else
            {
                var entitys = entity.FirstOrDefault();
                var sss= ObjectMapper.Map<IHousingUpdateDTO, RatePlans >(dto, entitys);
                await _RatePlansReservationsrepository.UpdateAsync(sss);
                return ApiResult.Ok();
            }
        }


    }
}
